package com.wnhz.work.yz.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.style.AlignmentSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.utils.L;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.HetongBean;
import com.wnhz.work.yz.utils.Prefer;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class StandardPactActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_jiafang, et_address, et_phone, et_chuanzhen, et_goodsAddress, et_name, et_code;

    private String record_id;
    private String pay_way;
    private String cartIds;
    private TextView text_right;

    private final int STANDARD_PACT = 555;

    public static Intent createIntent(Context context, String record_id, String pay_way, String cartIds) {
        Intent intent = new Intent(context, StandardPactActivity.class);
        intent.putExtra("record_id", record_id);
        intent.putExtra("pay_way", pay_way);
        intent.putExtra("cartIds", cartIds);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        record_id = intent.getStringExtra("record_id");
        pay_way = intent.getStringExtra("pay_way");
        cartIds = intent.getStringExtra("cartIds");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard_pact);
        parseIntent();
        initTitle();
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("标准合同");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_chakan).setOnClickListener(this);
        text_right = (TextView) findViewById(R.id.view_main_right_text);
        text_right.setText("回填");
        text_right.setVisibility(View.VISIBLE);
        text_right.setOnClickListener(this);

        et_jiafang = (EditText) findViewById(R.id.et_jiafang);
        et_address = (EditText) findViewById(R.id.et_address);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_chuanzhen = (EditText) findViewById(R.id.et_chuanzhen);
        et_goodsAddress = (EditText) findViewById(R.id.et_goodsAddress);
        et_name = (EditText) findViewById(R.id.et_name);
        et_code = (EditText) findViewById(R.id.et_code);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.view_main_right_text:
                getData();
                break;
            case R.id.tv_chakan:
                if (TextUtils.isEmpty(et_jiafang.getText().toString())) {
                    MyToast("合同甲方不能为空");
                    return;
                }

                if (TextUtils.isEmpty(et_address.getText().toString())) {
                    MyToast("合同甲方地址不能为空");
                    return;
                }

                if (TextUtils.isEmpty(et_phone.getText().toString())) {
                    MyToast("合同甲方电话不能为空");
                    return;
                }

                if (TextUtils.isEmpty(et_goodsAddress.getText().toString())) {
                    MyToast("收货地址不能为空");
                    return;
                }

                if (TextUtils.isEmpty(et_chuanzhen.getText().toString())) {
                    MyToast("传真不能为空");
                    return;
                }

                if (TextUtils.isEmpty(et_name.getText().toString())) {
                    MyToast("收货人不能为空");
                    return;
                }

                if (TextUtils.isEmpty(et_code.getText().toString()) || et_code.getText().toString().length() != 18) {
                    MyToast("请填写正确的身份证号码");
                    return;
                }

                Map<String, Object> map = new HashMap<>();
                map.put("record_id", record_id);
                map.put("pay_way", "" + pay_way);

                Log.e("===合同的付款方式====", "" + pay_way);
                map.put("cartIds", cartIds);
                map.put("hetong_name", et_jiafang.getText().toString());
                map.put("hetong_address", et_address.getText().toString());
                map.put("hetong_tel", et_phone.getText().toString());

                if (!TextUtils.isEmpty(et_chuanzhen.getText().toString())) {
                    map.put("hetong_fax", et_chuanzhen.getText().toString());
                }

                map.put("hetong_addr", et_goodsAddress.getText().toString());
                map.put("hetong_realname", et_name.getText().toString());
                map.put("hetong_number", et_code.getText().toString());

                XUtil.Post(Url.CHANKAN, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        Log.e(" =查看合同  ==", result);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
                            String info = jsonObject.optString("info");
                            if (res.equals("1")) {

                                Prefer.getInstance().setHTAddress(et_address.getText().toString());
                                Prefer.getInstance().setHTJiaFang(et_jiafang.getText().toString());
                                Prefer.getInstance().setHTPhone(et_phone.getText().toString());
                                Prefer.getInstance().setHTChuanZhen(et_chuanzhen.getText().toString());
                                Prefer.getInstance().setHTJioaHuo(et_goodsAddress.getText().toString());
                                System.out.println("et_goodsAddress.getText().toString()==="+et_goodsAddress.getText().toString());
                                Prefer.getInstance().setHTShouHuoren(et_name.getText().toString());
                                Prefer.getInstance().setHTShenFenzheng(et_code.getText().toString());

                                startActivityForResult(PactDetailsActivity.createIntent(StandardPactActivity.this, info), STANDARD_PACT);
                            } else if (res.equals("0")) {
                                MyToast(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

        }
    }

    private void getData() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());

        XUtil.Post(Url.BUDGET_LASTHETONGDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("===合同信息====", result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if (res.equals("1")) {
                        Gson gson = new Gson();
                        HetongBean hetongBean = gson.fromJson(result, HetongBean.class);
                        et_jiafang.setText(hetongBean.getInfo().getHetong_name());
                        et_address.setText(hetongBean.getInfo().getHetong_address());
                        et_phone.setText(hetongBean.getInfo().getHetong_tel());
                        et_chuanzhen.setText(hetongBean.getInfo().getHetong_fax());
                        et_goodsAddress.setText(hetongBean.getInfo().getHetong_addr());
                        et_name.setText(hetongBean.getInfo().getHetong_realname());
                        et_code.setText(hetongBean.getInfo().getHetong_number());
                    } else if (res.equals("0")) {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case STANDARD_PACT:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        setResult(200);
                        finish();
                    }
                }
                break;

            default:
                break;
        }
    }
}
